/*********************************
 * Themes, rules, and i18n support
 * Locale: Chinese; 中文
 *********************************/
(function (factory) {
    'function' === typeof define && (define.amd || define.cmd) ? define(function (require, exports, module) {
            var $ = require('jquery') || jQuery;
            $._VALIDATOR_URI = module.uri;
            require('../jquery.validator.min')($);
            factory($);
        }) : factory(jQuery);
}(function ($) {

    /* Global configuration
     *
     */
    $.validator.config({
        //stopOnError: true,
        //focusCleanup: true,
        //theme: 'yellow_right',
        //timely: 2,

        // Custom rules 其他常见规则：https://validator.niceue.com/docs/common-rules.html
        rules: {
            digits: [/^\d+$/, "请输入数字"]
            ,
            letters: [/^[a-z]+$/i, "请填写字母"]
            ,
            date: [/^\d{4}-\d{2}-\d{2}$/, "请填写有效的日期，格式:yyyy-mm-dd"]
            ,
            time: [/^([01]\d|2[0-3])(:[0-5]\d){1,2}$/, "请填写有效的时间，00:00到23:59之间"]
            ,
            email: [/^[\w\+\-]+(\.[\w\+\-]+)*@[a-z\d\-]+(\.[a-z\d\-]+)*\.([a-z]{2,4})$/i, "请填写有效的邮箱"]
            ,
            url: [/^(https?|s?ftp):\/\/\S+$/i, "请填写有效的网址"]
            ,
            qq: [/^[1-9]\d{4,}$/, "请填写有效的QQ号"]
            ,
            IDcard: [/^\d{6}(19|2\d)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)?$/, "请填写正确的身份证号码"]
            ,
            tel: [/^(?:(?:0\d{2,3}[\- ]?[1-9]\d{6,7})|(?:[48]00[\- ]?[1-9]\d{6}))$/, "请填写有效的电话号码"]
            ,
            mobile: [/^1[3-9]\d{9}$/, "请填写有效的手机号"]
            ,
            zipcode: [/^\d{6}$/, "请检查邮政编码格式"]
            ,
            chinese: [/^[\u0391-\uFFE5]+$/, "请填写中文字符"]
            ,
            username: [/^\w{3,12}$/, "请填写3-12位数字、字母、下划线"]
            ,
            password: [/^[\S]{6,16}$/, "请填写6-16位字符，不能包含空格"]
            ,
            number: [/^(-?\d+)(\.\d+)?$/, "请输入有效数字"]
            ,
            ID_card: [/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[A-Z])$/, "请输入正确的身份证号码"]
            ,
            postcode: [/^[1-9]\d{5}$/, "邮政编码格式不正确"]
            ,
            plus: [/^[0-9]*[0-9][0-9]*$/, "请输入一个正整数"]
            ,
            mobile_tel: [/^(?:(?:0\d{2,3}[\- ]?[1-9]\d{6,7})|(?:[48]00[\- ]?[1-9]\d{6}))$|^(1[3-9]\d{9})$/, "请输入正确的手机号码或者电话号码"]
            ,
            money: [/^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/, "请输入正确的数字"]
            ,
            sort: [/^([1-6])?$/, "请输入1-6的数字"]
            ,
            exchangeCode: [/^\s*\d{16}\s*$/, "请输入16位数字的兑换码"]
            ,
            propotion: [/^(0|1|1\.[0]*|0?\.[\d]+)$/, "请输入0到1之间的数字"]
            ,
            format: [/^(\d*,*)*$/, "请输入格式 数字,数字"]
            ,
            startWeigt: [/^\d+(\.\d{1})?$/, "只能输入正一位小数、整数"]
            ,
            starWeigt: [/^\d+(\.\d{1,2})?$/, "只能输入正两位小数、整数"]
            ,
            standardwight: [/^([1-9][\d]{0,3}|0)(\.[\d]{1,2})?$/, "最大9999.99"]
            ,
            plusNum: [/^[0-9]\d*(\.\d+)?$/, "必须大于0"]
            ,
            plusNums: [/^[1-9]\d*(\.\d+)?$/, "必须大于0"]
            ,
            plusnums: [/^([1-9][\d]{0,1}|0)(\.[\d]{1,4})?$/, "1到100之间，小数点后最多4位"]
            ,
            plusnums1: [/^1$|^0(\.\d{1,3})?$/, "0.001-1范围内的数字，小数位最多3位"]
		    ,
		    plusnums2: [/^1$|^0(\.\d{1,})?$/, "0到1之间，可以是小数"]
		    ,
            shopno: [/^\w{1,12}$/, "格式不正确"]
            ,
            nozero: [/^(\+|-)?([1-9][0-9]*(\.\d+)?|(0\.(?!0+$)\d+))$/, "必须大于0"]
            ,
            chineseletternum: [/^[\u4E00-\u9FA5A-Za-z0-9]{4,20}$/, "请输入4到20位中英文、数字"]
            ,
            creditcard: [/^\d+$/, "请输入合法的信用卡号"]
            ,
            accept: function (element, params) {
                if (!params) return true;
                var ext = params[0];
                return (ext === '*') ||
                    (new RegExp(".(?:" + (ext || "png|jpg|jpeg|gif") + ")$", "i")).test(element.value) ||
                    this.renderMsg("只接受{1}后缀", ext.replace('|', ','));
            }
        },

        // Default error messages
        messages: {
            0: "此处",
            fallback: "{0}格式不正确",
            loading: "正在验证...",
            error: "网络异常",
            timeout: "请求超时",
            required: "{0}不能为空",
            remote: "{0}已被使用",
            integer: {
                '*': "请填写整数",
                '+': "请填写正整数",
                '+0': "请填写正整数或0",
                '-': "请填写负整数",
                '-0': "请填写负整数或0"
            },
            match: {
                eq: "{0}与{1}不一致",
                neq: "{0}与{1}不能相同",
                lt: "{0}必须小于{1}",
                gt: "{0}必须大于{1}",
                lte: "{0}不能大于{1}",
                gte: "{0}不能小于{1}"
            },
            range: {
                rg: "请填写{1}到{2}的数",
                gte: "请填写不小于{1}的数",
                lte: "请填写最大{1}的数",
                gtlt: "请填写{1}到{2}之间的数",
                gt: "请填写大于{1}的数",
                lt: "请填写小于{1}的数"
            },
            checked: {
                eq: "请选择{1}项",
                rg: "请选择{1}到{2}项",
                gte: "请至少选择{1}项",
                lte: "请最多选择{1}项"
            },
            length: {
                eq: "请填写{1}个字符",
                rg: "请填写{1}到{2}个字符",
                gte: "请至少填写{1}个字符",
                lte: "请最多填写{1}个字符",
                eq_2: "",
                rg_2: "",
                gte_2: "",
                lte_2: ""
            }
        },

        /**
         * {0} 寻找规则
         *
         * 1:优先找：data-rule="分类名称:required;"
         * 2:然后找 display的函数
         * 3:最后找 0: "此处",
         *
         * 参见教程：//https://niceue.com/validator/#plugin_config  display
         * @author daundazhi
         * @date 2017-01-09
         * @param element
         */
        display: function (element) {
            return $(element).parent().prev('label').text();
        }

    });

    /* Themes
     */
    var TPL_ARROW = '<span class="n-arrow"><b>◆</b><i>◆</i></span>';
    $.validator.setTheme({
        'simple_right': {
            formClass: 'n-simple',
            msgClass: 'n-right'
        },
        'simple_bottom': {
            formClass: 'n-simple',
            msgClass: 'n-bottom'
        },
        'yellow_top': {
            formClass: 'n-yellow',
            msgClass: 'n-top',
            msgArrow: TPL_ARROW
        },
        'yellow_right': {
            formClass: 'n-yellow',
            msgClass: 'n-right',
            msgArrow: TPL_ARROW
        },
        'yellow_right_effect': {
            formClass: 'n-yellow',
            msgClass: 'n-right',
            msgArrow: TPL_ARROW,
            msgShow: function ($msgbox, type) {
                var $el = $msgbox.children();
                if ($el.is(':animated')) return;
                if (type === 'error') {
                    $el.css({left: '20px', opacity: 0})
                        .delay(100).show().stop()
                        .animate({left: '-4px', opacity: 1}, 150)
                        .animate({left: '3px'}, 80)
                        .animate({left: 0}, 80);
                } else {
                    $el.css({left: 0, opacity: 1}).fadeIn(200);
                }
            },
            msgHide: function ($msgbox, type) {
                var $el = $msgbox.children();
                $el.stop().delay(100).show()
                    .animate({left: '20px', opacity: 0}, 300, function () {
                        $msgbox.hide();
                    });
            }
        }
    });
}));
